package bb.lanxing.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class HeaderAdapterWrapper extends AbsHeaderAdapter {
    private final IRVHeaderAdapter.HeaderViewHolderBinder mHeaderViewBinder;
    private final RecyclerView.Adapter<RecyclerView.ViewHolder> mWrappedAdapter;

    public HeaderAdapterWrapper(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter, IRVHeaderAdapter.HeaderViewHolderBinder binder) {
        mWrappedAdapter = adapter;
        mHeaderViewBinder = binder;
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                notifyDataSetChanged();
            }

            @Override
            public void onItemRangeChanged(int positionStart, int itemCount) {
                notifyItemRangeChanged(positionStart + getHeaderCount(), itemCount);
            }

            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                notifyItemRangeInserted(positionStart + getHeaderCount(), itemCount);
            }

            @Override
            public void onItemRangeRemoved(int positionStart, int itemCount) {
                notifyItemRangeRemoved(positionStart + getHeaderCount(), itemCount);
            }

            @Override
            public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
                for (int i = 0; i < itemCount; i++) {
                    notifyItemMoved(getHeaderCount() + fromPosition, getHeaderCount() + toPosition);
                }
            }
        });
    }

    public HeaderAdapterWrapper(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
        this(adapter, null);
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return generateHeaderType(position);
        }
        if (isFooterView(position)) {
            return generateFooterType(position);
        }
        return this.mWrappedAdapter.getItemViewType(position - getHeaderCount());
    }

    @Override
    public int getItemCount() {
        return getHeaderCount() + this.mWrappedAdapter.getItemCount() + getFooterCount();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int i = (-65536) & viewType;
        if (i == 65536) {
            int i2 = viewType & 65535;
            View headerView = getHeaderView(i2);
            IRVHeaderAdapter.HeaderViewHolderBinder headerViewHolderBinder = this.mHeaderViewBinder;
            if (headerViewHolderBinder != null) {
                return headerViewHolderBinder.onCreateHeaderViewHolder(headerView, i2);
            }
            if (mWrappedAdapter instanceof IRVHeaderAdapter.HeaderViewHolderBinder) {
                return ((IRVHeaderAdapter.HeaderViewHolderBinder) mWrappedAdapter).onCreateHeaderViewHolder(headerView, i2);
            }
            return new HeaderViewHolder(headerView);
        } else if (i == 131072) {
            int i3 = viewType & 65535;
            View footerView = getFooterView(i3);
            IRVHeaderAdapter.HeaderViewHolderBinder headerViewHolderBinder2 = this.mHeaderViewBinder;
            if (headerViewHolderBinder2 != null) {
                return headerViewHolderBinder2.onCreateFooterViewHolder(footerView, i3);
            }
            if (mWrappedAdapter instanceof IRVHeaderAdapter.HeaderViewHolderBinder) {
                return ((IRVHeaderAdapter.HeaderViewHolderBinder) mWrappedAdapter).onCreateFooterViewHolder(footerView, i3);
            }
            return new HeaderViewHolder(footerView);
        } else {
            return this.mWrappedAdapter.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (isHeaderView(position)) {
            IRVHeaderAdapter.HeaderViewHolderBinder headerViewHolderBinder = this.mHeaderViewBinder;
            if (headerViewHolderBinder != null) {
                headerViewHolderBinder.onBindHeaderViewHolder(holder, position);
                return;
            }
            if (mWrappedAdapter instanceof IRVHeaderAdapter.HeaderViewHolderBinder) {
                ((IRVHeaderAdapter.HeaderViewHolderBinder) mWrappedAdapter).onBindHeaderViewHolder(holder, position);
            }
        } else if (isFooterView(position)) {
            IRVHeaderAdapter.HeaderViewHolderBinder headerViewHolderBinder2 = this.mHeaderViewBinder;
            if (headerViewHolderBinder2 != null) {
                headerViewHolderBinder2.onBindFooterViewHolder(holder, position);
                return;
            }
            if (mWrappedAdapter instanceof IRVHeaderAdapter.HeaderViewHolderBinder) {
                ((IRVHeaderAdapter.HeaderViewHolderBinder) this.mWrappedAdapter).onBindFooterViewHolder(holder, position - (mWrappedAdapter.getItemCount() + getHeaderCount()));
            }
        } else {
            mWrappedAdapter.onBindViewHolder(holder, position - getHeaderCount());
        }
    }

    @Override
    public void setHasStableIds(boolean hasStableIds) {
        mWrappedAdapter.setHasStableIds(hasStableIds);
    }

    @Override
    public long getItemId(int position) {
        return mWrappedAdapter.getItemId(position);
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        mWrappedAdapter.onViewRecycled(holder);
    }

    @Override
    public boolean onFailedToRecycleView(@NonNull RecyclerView.ViewHolder holder) {
        return mWrappedAdapter.onFailedToRecycleView(holder);
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        mWrappedAdapter.onViewAttachedToWindow(holder);
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        mWrappedAdapter.onViewDetachedFromWindow(holder);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        mWrappedAdapter.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        mWrappedAdapter.onDetachedFromRecyclerView(recyclerView);
    }

    public RecyclerView.Adapter<RecyclerView.ViewHolder> getWrappedAdapter() {
        return mWrappedAdapter;
    }
}
